package ng.test;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

import ng.lib.Vector2D;
import ng.map.Map;
import ng.map.MapObjectDynamic;
import ng.map.Rocket;
import ng.map.Spacecraft;

public class LeMap extends JFrame {

	public final static int TICK_DURING = 20;
	private final JFrame parent = this;
	private final Map map;

	public LeMap() {
		this.setSize(850, 640);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.map = new Map(800, 600);
		final MapObjectDynamic o1 = new Spacecraft(new Vector2D(100, 100));
		final MapObjectDynamic o2 = new Spacecraft(new Vector2D(400, 100));
		
		this.map.addMapObject(o1);
		this.map.addMapObject(o2);
		final LeMap parent = this;
		// this.map.addMapObject(o3);

		this.add(new JPanel() {
			private static final long serialVersionUID = 1L;

			// Exemplat Initialisierung
			{
				this.setBounds(25, 25, 800, 600);
			}

			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				Graphics2D g2 = (Graphics2D) g;
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				map.draw(g2);
			}

		});

		this.addKeyListener(new KeyAdapter() {

			
			@Override
			public void keyPressed(KeyEvent e) {
				int k = e.getKeyCode();
				// System.out.println(k);

				switch (k) {
				case 65:// a
					o1.setAngularRate(-0.1);
					break;
				case 87:// w
					o1.getAcceleration().add(new Vector2D(0,-1).rotate(o1.getAngle()));
					break;
				case 68:// d
					o1.setAngularRate(0.1);
					break;
				case KeyEvent.VK_LEFT:
					o2.setAngularRate(-0.1);
					break;
				case KeyEvent.VK_UP:// w
					o2.getAcceleration().add(new Vector2D(0,-1).rotate(o2.getAngle()));
					break;
				case KeyEvent.VK_RIGHT:// d
					o2.setAngularRate(0.1);
					break;
				case KeyEvent.VK_ENTER:// d
					MapObjectDynamic p = new Rocket(o2.getPosition().clone());
					p.setSpeed(new Vector2D(0,-1).rotate(o2.getAngle()).multiply(8));
					parent.map.addMapObject(p);
					break;
				case KeyEvent.VK_S:// d
					MapObjectDynamic p2 = new Rocket(o1.getPosition().clone());
					p2.setSpeed(new Vector2D(0,-1).rotate(o1.getAngle()).multiply(8));
					parent.map.addMapObject(p2);
					break;
		
				}

			}

			@Override
			public void keyReleased(KeyEvent e) {
				int k = e.getKeyCode();
				// System.out.println(k);

				switch (k) {
				case 65:// a
					o1.setAngularRate(0);
					break;
				case 87:// w
					o1.getAcceleration().x = 0;
					o1.getAcceleration().y = 0;
					break;
				case 68:// d
					o1.setAngularRate(0);
					break;
				case KeyEvent.VK_LEFT:
					o2.setAngularRate(0);
					break;
				case KeyEvent.VK_UP:// w
					o2.getAcceleration().x = 0;
					o2.getAcceleration().y = 0;
					break;
				case KeyEvent.VK_RIGHT:// d
					o2.setAngularRate(0);
					break;
			
				}
			}
		});

		new Thread(new Runnable() {
			public void run() {
				while (true) {
					try {
						Thread.sleep(TICK_DURING);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					map.tick();
					parent.repaint();
				}
			}
		}).start();
	}

	public static void main(String[] args) {
		new LeMap();
	}

}
